# -*- coding: utf-8 -*-
# author:HHR
# time:2021/1/14:18:10
# ___msg___ = '抓取案件状态'
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from quote_folder.Db_connect import DB_connect
from selenium.webdriver.common.by import By
from selenium import webdriver
from bs4 import BeautifulSoup
import time, random, re



class annual_msg():
	def __init__(self):
		self.get_url = "http://cpquery.cnipa.gov.cn/"
		self.develop_con = DB_connect().online_db()				# 开发数据库连接
		self.develop_cur = self.develop_con.cursor()
		self.bigdata_con = DB_connect().online_big_data()		# 数据组数据库连接
		self.bigdata_cur = self.bigdata_con.cursor()
		self.redis_con = DB_connect().patents_reids()			# redis连接

	def login_gov(self):
		""" 登录中国及多国 """
		profile = webdriver.FirefoxOptions()
		profile.set_preference('network.proxy.type', 1)
		browser = webdriver.Firefox(options=profile, executable_path=r'D:\谷歌下载\geckodriver-v0.30.0-win64\geckodriver.exe')
		browser.get(self.get_url)
		time.sleep(5)
		WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "username1"))).clear()
		# browser.find_element_by_id('username').clear()
		browser.find_element_by_id('username1').send_keys('18071440149')
		time.sleep(random.randint(5, 10))
		browser.find_element_by_id('password1').clear()
		browser.find_element_by_id('password1').send_keys('Xhhg123456!')
		time.sleep(random.randint(30, 50))
		current_url = browser.current_url
		return browser, current_url

	def execute_sql(self, insert_sql):
		""" 执行sql插入 """
		try:
			self.bigdata_cur.execute(insert_sql)
		except Exception as E:
			print(insert_sql)
			print("插入错误：", E)

	def get_payment_information(self, soup, sqh, now_time):
		""" 获取缴费信息 """
		jf_div_list = soup.select("span[name='record_zlx:anjianywzt']")
		caseStatus = jf_div_list[0]["title"]			# 案件状态

		jf_list = []

		return jf_list



	def get_msg(self, sqh, browser, current_url, now_time):
		sqh = re.sub(r'ZL', '', sqh)
		pat = ''.join(re.findall(r'select-key:shenqingh=(.*?)&select-key:backPage', current_url))
		url = re.sub(pat, sqh, current_url)
		browser.get(url)
		count_page = browser.page_source
		soup = BeautifulSoup(count_page, "lxml")
		jf_item = self.get_payment_information(soup, sqh, now_time)		# 获取缴费信息

	def main_fun(self):
		now_time = time.strftime("%m%d", time.localtime())
		# now_time = "0516"
		browser, current_url = self.login_gov()
		user_dict = [
			{"username": "15027399434", "password": "YJyj!1234"},
			{"username": "18071440149", "password": "Xhhg123456!"},
			{"username": "15978554102", "password": "Hhr31415926!"},
			{"username": "", "password": ""}
		]

		for application_number in []:
			self.get_msg(application_number, browser, current_url, now_time)			# 获取专利缴费信息
			self.bigdata_con.commit()
			print()
			print()

		self.bigdata_con.commit()
		self.bigdata_cur.close()
		self.bigdata_con.close()
		self.develop_cur.close()
		self.develop_con.close()


if __name__ == '__main__':
	annual_msg().main_fun()		# 2020207090033
